/*
 * @FilePath: /tads/server/app/business/service/tactics.go
 * @Author: wnina0x0
 * @Date: 2025-04-14 13:42:05
 * @Description:
 *
 * 版权信息 : 2025 by wnina0x0, All Rights Reserved.
 */
package service

import (
	"fmt"
	"log"
	"strconv"
	"strings"
	"tads/server/app/business/models"
	"tads/server/app/business/service/dto"
	"tads/server/core/sdk/service"
)

type Tactics struct {
	service.Service
}

func (s *Tactics) Inset(d *dto.TacticsInsetRequests) error {
	var err error
	var data models.Tactics
	d.Generate(&data)
	if err = s.Postgres.Create(&data).Error; err != nil {
		return err
	}
	return nil
}

func (s *Tactics) GetScene(d *dto.TacticsGetRequests) (*dto.TacticsGetResponse, error) {
	var err error
	var data []models.Tactics
	count := 0
	resps := make([]dto.TacticsGetData, 0)
	res := dto.TacticsGetResponse{}
	if d.PageIndex <= 0 || d.PageSize <= 0 {
		d.PageIndex = 1
		d.PageSize = 10
	}
	limit := d.PageSize
	offset := (d.PageIndex - 1) * d.PageSize
	if err = s.Postgres.Offset(offset).Where(" scene_name ILIKE ? ", fmt.Sprintf("%%%s%%", d.TacticsName)).Limit(limit).Find(&data).Error; err != nil {
		return res.NewPageResponse(count, d, resps), err
	}
	count = len(data)
	if count == 0 {
		return res.NewPageResponse(count, d, resps), nil
	}
	resp := &dto.TacticsGetData{}
	bus := []string{}
	tactics_type := []string{}
	for _, value := range data {
		bus, err = s.GetBusType(value.TacticsDbusTypeText)
		if err != nil {
			return res.NewPageResponse(count, d, resps), err
		}
		tactics_type, err = s.GetTacticsType(value.TacticsAttackMethodIdText)
		if err != nil {
			return res.NewPageResponse(count, d, resps), err
		}
		resp = res.GetData(&value, bus, tactics_type)
		resps = append(resps, *resp)
	}

	return res.NewPageResponse(count, d, resps), nil
}

func (s *Tactics) GetBusType(bus string) ([]string, error) {
	var err error
	var data models.BusType
	busList := []uint{}
	res := []string{}
	for _, value := range strings.Split(bus, ",") {
		num16, _ := strconv.ParseUint(value, 10, 16)
		busList = append(busList, uint(num16))
	}
	if err = s.Postgres.Model(&data).Where("id IN ? AND is_deleted = ?", busList, false).Select("bus_type_name").Pluck("bus_type_name", &res).Error; err != nil {
		return res, err
	}
	return res, nil
}

func (s *Tactics) GetTacticsType(tactics_type string) ([]string, error) {
	var err error
	var data models.TacticsType
	tacticsTypeList := []uint{}
	res := []string{}
	for _, value := range strings.Split(tactics_type, ",") {
		num16, _ := strconv.ParseUint(value, 10, 16)
		tacticsTypeList = append(tacticsTypeList, uint(num16))
	}
	if err = s.Postgres.Model(&data).Where("id IN ? AND is_deleted = ?", tacticsTypeList, false).Select("tactic_type_name").Pluck("tactic_type_name", &res).Error; err != nil {
		return res, err
	}
	return res, nil
}

func (s *Tactics) Update(q *dto.TacticsUpdateRequests) error {
	var err error
	var data models.Tactics
	q.Generate(&data)
	if err = s.Postgres.Where("id = ?", q.ID).Updates(&data).Error; err != nil {
		return err
	}
	return nil
}

func (s *Tactics) Status(q *dto.TacticsStatusRequests) error {
	var err error
	var data models.Tactics
	log.Println(q.ID, q.Status)
	if err = s.Postgres.Model(&data).Where("id = ?", q.ID).Update("is_deleted", q.Status).Error; err != nil {
		return err
	}
	return nil
}
